package com.example.partner.activity.home;

import static com.example.partner.common.ConfigConstant.BASE_URL;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.partner.R;
import com.example.partner.common.BaseResponse;
import com.example.partner.common.Page;
import com.example.partner.model.domain.User;
import com.example.partner.adapter.user.UserAdapter;
import com.example.partner.service.UserService;

import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserListActivity extends AppCompatActivity {
    private UserService userService;
    EditText searchText;
    Button searchButton;
    ListView listView;
    UserAdapter adapter;
    @SuppressLint({"MissingInflatedId", "NonConstantResourceId"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);
        listView = findViewById(R.id.userList);
        searchText = findViewById(R.id.searchText);
        searchButton = findViewById(R.id.searchButton);

        // 初始化Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL) // 替换为您的服务器地址
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        userService = retrofit.create(UserService.class);
        searchUsers("");
        searchButton.setOnClickListener(view -> {
            String username = searchText.getText().toString();
            searchUsers(username); // 调用搜索用户的方法
        });

        listView.setOnItemClickListener((adapterView, view, position, l) -> {
            // 获取点击的项
            User selectedUser = (User) adapter.getItem(position);

            Long id=selectedUser.getId();
            System.out.println(id);
            String username = selectedUser.getUsername();
            String userTags = selectedUser.getTags();
            String userProfile = selectedUser.getProfile();
            String userPhone = selectedUser.getPhone();
            String userRole = String.valueOf(selectedUser.getUserRole());
            // 创建Intent并启动新的Activity
            Intent intent = new Intent(UserListActivity.this, UserInfoActivity.class);
//            Bundle bundle = new Bundle();
//            bundle.putSerializable("user", selectedUser);
//            intent.putExtras(bundle);
            intent.putExtra("id",id);
            intent.putExtra("username", username);
            intent.putExtra("userTags", userTags);
            intent.putExtra("userProfile", userProfile);
            intent.putExtra("userPhone", userPhone);
            intent.putExtra("userRole", userRole);
            startActivity(intent);
        });


    }


    private void searchUsers(String query) {
        userService.userSearch(query).enqueue(new Callback<BaseResponse<Page<User>>>() {
            @Override
            public void onResponse(Call<BaseResponse<Page<User>>> call, Response<BaseResponse<Page<User>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    // 处理成功的响应
                    Page<User> users = response.body().getData();
                    List<User> userList = users.getRecords();
                    adapter = new UserAdapter(UserListActivity.this, userList);
                    listView.setAdapter(adapter);
                    // 更新UI或处理数据
                } else {
                    // 处理错误情况
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<Page<User>>> call, Throwable t) {
                // 网络错误处理
                t.printStackTrace();
            }
        });
    }
}
